| Conditions | 4 |
| Paths | 32 |
| Total Lines | 104 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 75 | function RestClient (settings, transport) { |
||
| 76 | var self = this |
||
| 77 | var opts = getDefaults() |
||
| 78 | var client |
||
| 79 | var logger |
||
| 80 | var counter = 0 |
||
| 81 | |||
| 82 | if (settings instanceof Function) { |
||
| 83 | transport = settings |
||
| 84 | settings = {} |
||
| 85 | } |
||
| 86 | settings = settings || {} |
||
| 87 | if (typeof settings === 'string' || settings instanceof String) { |
||
| 88 | settings = {url: settings} |
||
| 89 | } |
||
| 90 | Object.keys(opts).forEach(function (key) { |
||
| 91 | if (key in settings) { |
||
| 92 | opts[key] = settings[key] |
||
| 93 | } |
||
| 94 | }) |
||
| 95 | client = settings.client || new Client(opts, transport) |
||
| 96 | logger = Slf4j.factory(opts.logger, 'ama-team.voxengine-sdk.http.rest') |
||
| 97 | |||
| 98 | function execute (request) { |
||
| 99 | var id = ++counter |
||
| 100 | request.id = request.id || id |
||
| 101 | var basicRequest = { |
||
| 102 | url: request.resource || request.route, // 0.2.0 compatibility |
||
| 103 | method: request.method, |
||
| 104 | query: request.query, |
||
| 105 | headers: request.headers, |
||
| 106 | timeout: typeof request.timeout === 'number' ? request.timeout : settings.timeout |
||
| 107 | } |
||
| 108 | logger.debug('Executing request #{} `{} {}`', request.id, request.method, request.resource) |
||
| 109 | basicRequest.payload = request.payload ? opts.serializer.serialize(request.payload) : null |
||
| 110 | return client |
||
| 111 | .execute(basicRequest) |
||
| 112 | .then(function (response) { |
||
| 113 | logger.debug('Request #{} `{} {}` received response with code {}', |
||
| 114 | request.id, request.method, request.resource, response.code) |
||
| 115 | return { |
||
| 116 | code: response.code, |
||
| 117 | payload: response.payload ? opts.serializer.deserialize(response.payload) : null, |
||
| 118 | headers: response.headers, |
||
| 119 | request: request |
||
| 120 | } |
||
| 121 | }, function (e) { |
||
| 122 | logger.debug('Request #{} `{} {}` has finished with error {}', |
||
| 123 | request.id, request.method, request.resource, e.name) |
||
| 124 | throw e |
||
| 125 | }) |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @inheritDoc |
||
| 130 | */ |
||
| 131 | function request (method, resource, payload, query, headers, timeout) { |
||
| 132 | return execute({resource: resource, method: method, query: query, payload: payload, headers: headers, timeout: timeout}) |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @inheritDoc |
||
| 137 | */ |
||
| 138 | this.execute = execute |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @inheritDoc |
||
| 142 | */ |
||
| 143 | this.request = request |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @inheritDoc |
||
| 147 | */ |
||
| 148 | this.exists = function (resource, query, headers, timeout) { |
||
| 149 | return request(M.Head, resource, null, query, headers, timeout) |
||
| 150 | .then(function (response) { |
||
| 151 | return response.code !== 404 |
||
| 152 | }) |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @inheritDoc |
||
| 157 | */ |
||
| 158 | this.get = function (resource, query, headers, timeout) { |
||
| 159 | return request(M.Get, resource, null, query, headers, timeout) |
||
| 160 | .then(function (response) { |
||
| 161 | return response.code === 404 ? null : response.payload |
||
| 162 | }) |
||
| 163 | } |
||
| 164 | |||
| 165 | var methods = {create: M.Post, set: M.Put, modify: M.Patch, delete: M.Delete} |
||
| 166 | Object.keys(methods).forEach(function (method) { |
||
| 167 | self[method] = function (resource, payload, headers, query, timeout) { |
||
| 168 | return request(methods[method], resource, payload, query, headers, timeout) |
||
| 169 | .then(function (response) { |
||
| 170 | if (response.code === 404) { |
||
| 171 | var message = 'Modification request has returned 404 status code' |
||
| 172 | throw new C.NotFoundException(message, response.request, response) |
||
| 173 | } |
||
| 174 | return response.payload |
||
| 175 | }) |
||
| 176 | } |
||
| 177 | }) |
||
| 178 | } |
||
| 179 | |||
| 204 |